package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */

    @PostMapping
    public R saveSetmeal(@RequestBody SetmealDto setmealDto){

        R r = setmealService.saveSetmeal(setmealDto);

        return r;
    }


    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R getSetmealByPage(Integer page,Integer pageSize,String name){

        Page<Setmeal> PageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        setmealService.page(PageInfo,qw);

        List<Setmeal> records = PageInfo.getRecords();
        List<SetmealDto> list = records.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(PageInfo,setmealDtoPage,"records");

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }


    /**
     * 删除套餐和批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteSetmeal(@RequestParam List<Long> ids){

        R r = setmealService.deleteSetmeal(ids);

        return r;
    }

    /**
     * 套餐的状态修改和批量修改操作
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable int status,@RequestParam List<Long> ids){

        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    /**
     * 通过前端传递的id查询套餐并回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getSetmealById(@PathVariable Long id){

        R<SetmealDto> setmeal = setmealService.getSetmeal(id);

        return setmeal;
    }

    @PutMapping
    public R updateSetmealById(@RequestBody SetmealDto setmealDto){

        R r = setmealService.updateSetmeal(setmealDto);

        return r;
    }

    @GetMapping("/list")
    public R getSetmealByCategory(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        qw.eq(status!=null,Setmeal::getStatus,status);
        qw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(qw);

        return R.success(list);
    }
}
